package com.cn.service;

import com.cn.dao.WangLuWordDao;
import com.cn.po.WordWL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WangLuWordServiceImpl implements WangLuWordService{
    @Autowired
    private WangLuWordDao wangLuWordDao;

    @Override
    public boolean save(WordWL wordWL) {
        return wangLuWordDao.save(wordWL)>0;
    }

    @Override
    public boolean update(WordWL wordWL) {
        return wangLuWordDao.update(wordWL)>0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return wangLuWordDao.deleteById(id)>0;
    }

    @Override
    public WordWL getWordById(Integer id) {
        return wangLuWordDao.getWordById(id);
    }

    @Override
    public WordWL getByWord(String word) {
        return wangLuWordDao.getByWord(word);
    }

    @Override
    public List<WordWL> getAll() {
        return wangLuWordDao.getAll();
    }

    @Override
    public List<String> getDistinctChapters() {
        return wangLuWordDao.getDistinctChapters();
    }

    @Override
    public List<WordWL> getWordsByPage(String query, String chapter, int offset, int limit) {
        return wangLuWordDao.getWordsByPage(query, chapter, offset, limit);
    }

    @Override
    public int getTotalCount(String query, String chapter) {
        return wangLuWordDao.getTotalCount(query, chapter);
    }

    @Override
    public List<WordWL> getWordsByChapter(String chapter) {
        return wangLuWordDao.getWordsByChapter(chapter);
    }

    //@Override
    //public void updateErrorCounts(List<Integer> errorWordIds) {
    //    for (Integer wordId : errorWordIds) {
    //        wangLuWordDao.incrementErrorCount(wordId);
    //    }
    //}

    @Override
    public List<WordWL> getPaginatedErrorWords(int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        System.out.println("qunimd");
        List<WordWL> errorWords = wangLuWordDao.getPaginatedErrorWords(pageSize, offset);
        return errorWords;
    }

    @Override
    public int getErrorWordsCount() {
        int totalCount = wangLuWordDao.getErrorWordsCount();
        return totalCount;
    }


    //@Override
    //public void updateErrorCounts(List<Integer> correctWordIds, List<Integer> incorrectWordIds) {
    //    for (Integer wordId : correctWordIds) {
    //        wangLuWordDao.decrementErrorCount(wordId);
    //    }
    //    for (Integer wordId : incorrectWordIds) {
    //        wangLuWordDao.incrementErrorCount(wordId);
    //    }
    //}

    //  // 查询当前错误次数大于等于1的单词
    @Override
    public List<WordWL> getWordsWithErrors() {

        List<WordWL> wordsWithErrors = wangLuWordDao.findWordsWithErrors();
        return wordsWithErrors;
    }

    // 获取指定章节的错误单词
    @Override
    public List<WordWL> getErrorWordsByChapter(String chapter) {
        List<WordWL> chapterErrorWords = wangLuWordDao.findErrorWordsByChapter(chapter);
        return chapterErrorWords;
    }


    // 错词入库

    @Override
    public void updateErrorCounts(List<Integer> errorWordIds) {
        for (Integer errorWordId : errorWordIds) {
            //判断该单词在错词表中是否存在
            boolean result = wangLuWordDao.wordExists(errorWordId);
            // 如果存在该单词的错误记录，更新 current_error_count 和 history_error_count
            if (result){
                wangLuWordDao.updateErrorCount(errorWordId);
            }else{
                wangLuWordDao.insertErrorWord(errorWordId);
            }
        }
    }


    @Override
    public void updateErrorAndCorrectCounts(List<Integer> errorWordIds,List<Integer> correctIds) {
        for (Integer errorWordId : errorWordIds) {
            //判断该单词在错词表中是否存在
            boolean result = wangLuWordDao.wordExists(errorWordId);
            // 如果存在该单词的错误记录，更新 current_error_count 和 history_error_count
            if (result){
                wangLuWordDao.updateErrorCount(errorWordId);
            }else{
                wangLuWordDao.insertErrorWord(errorWordId);
            }
        }

        for(Integer correctId: correctIds){
            wangLuWordDao.updateCorrectCount(correctId);
        }
    }
}
